package com.bifrost.admin.controller;

import com.bifrost.annotation.CurrentAdmin;
import com.bifrost.annotation.CurrentStore;
import com.bifrost.common.base.ServerResponse;
import com.bifrost.entity.MerchantStore;
import com.bifrost.entity.User;
import com.bifrost.model.ResultModel;
import com.bifrost.model.ro.ProductImgUploadRO;
import com.bifrost.model.vo.PageVO;
import com.bifrost.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.http.util.Asserts;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 *  @Author:Sophia
 *  @Date:2018/4/28
 *  
 */
@RestController
@Api("后台商品上下架相关接口")
@RequestMapping("/admin/product")
public class ProductRackController {

    @Autowired
    ProductService productService;

    @ApiOperation("获取指定门店所有上下架商品")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "ids", value = "sap编码", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "productName", value = "商品名", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "enableUpload", value = "图片是否上传", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "auditStatus", value = "审核状态", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "startTime",value = "开始时间",required = false,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "endTime",value = "结束时间",required = false,dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "page", value = "当前页", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页数量", required = false, dataType = "String")
    })
    @GetMapping("/init/list")
    public ResultModel getInitCommodityProduct(@CurrentStore Long merchantId,String storeIds , String ids, String productName, String enableUpload, String auditStatus, String startTime,String endTime,@RequestParam(value = "page", defaultValue = "1") String page, @RequestParam(value = "pageSize", defaultValue = "10") String pageSize){
        PageVO pageVO=productService.getInitCommodityProduct(merchantId,storeIds,ids,productName,enableUpload,auditStatus,startTime,endTime,Integer.parseInt(page),Integer.parseInt(pageSize));
        if (pageVO!=null){
            return ResultModel.ok("成功").put("data",pageVO);
        }
        return ResultModel.error("未找到数据");
    }

    @ApiOperation("获取所有商品的审核状态")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "ids", value = "SKUID", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "productName", value = "商品名", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "auditStatus", value = "审核状态", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "startTime",value = "开始时间",required = false,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "endTime",value = "结束时间",required = false,dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "page", value = "当前页", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页数量", required = false, dataType = "String")
    })
    @GetMapping("/audit/list")
    public ResultModel getAuditCommodity(@CurrentStore Long merchantId,String storeIds , String ids, String productName, String auditStatus,String startTime,String endTime, @RequestParam(value = "page", defaultValue = "1") String page, @RequestParam(value = "pageSize", defaultValue = "10") String pageSize){
       PageVO pageVO=productService.getAuditCommodityProduct(merchantId,storeIds,ids,productName,auditStatus,startTime,endTime,Integer.parseInt(page),Integer.parseInt(pageSize));
        if (pageVO!=null){
            return ResultModel.ok("成功").put("data",pageVO);
        }
        return ResultModel.error("未找到数据");
    }

    @ApiOperation("审核商品")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "ids", value = "商品ID", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "auditStatus", value = "审核状态", required = true, dataType = "String")
    })
    @PostMapping("/audit/commodity")
    public ResultModel auditCommodity(@RequestParam String ids, @RequestParam String auditStatus){
        Asserts.notBlank(ids,"商品号不能为空");
       String message=productService.auditCommodityProduct(ids,auditStatus);
        if (message.equalsIgnoreCase("ok")){
            return ResultModel.ok("成功");
        }
        return ResultModel.error("失败");
    }

    @ApiOperation("保存并提交")
    @PostMapping("/keep/submit")
    public ResultModel keepAndSubmit(@CurrentAdmin Long userId , @RequestBody @NotEmpty(message = "参数不可以为空") @Valid List<ProductImgUploadRO> imgUploadVOs, HttpServletRequest request, BindingResult result){
        if(result.hasErrors()){
            return ResultModel.error(result.getAllErrors().get(0).getDefaultMessage());
        }
        try {
            productService.keepAndSubmit(userId,imgUploadVOs);
            return ResultModel.ok("成功");
        } catch (IOException e) {
            e.printStackTrace();
            return ResultModel.error("失败");
        }
    }

    @ApiOperation("商品上下热门")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "ids",value = "商品编号",dataType = "String",required = true),
            @ApiImplicitParam(paramType = "query",name = "hot",value = "是否热门",dataType = "Boolean",required = true)
    })
    @PostMapping("/fluctuate/hot")
    public ResultModel upOrDownHot(String ids,Boolean hot){
        try {
            productService.upOrDownHot(ids,hot);
            return ResultModel.ok("成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultModel.error("失败");
        }


    }
}
